package com.pox.guli.service.edu.controller.api;

import com.pox.guli.common.base.result.R;
import com.pox.guli.service.base.dto.CourseDto;
import com.pox.guli.service.edu.entity.Course;
import com.pox.guli.service.edu.entity.vo.ChapterVo;
import com.pox.guli.service.edu.entity.vo.WebCourseQueryVo;
import com.pox.guli.service.edu.entity.vo.WebCourseVo;
import com.pox.guli.service.edu.service.ChapterService;
import com.pox.guli.service.edu.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author PoX21s
 * @Date: 2022/6/5 11:03
 * @Version 1.0
 */

@Api(description = "前台课程展示")
@Slf4j
@RestController
//@CrossOrigin
@RequestMapping("/api/edu/course")
public class ApiCourseController {

    @Resource
    private CourseService courseService;
    @Resource
    private ChapterService chapterService;

    @ApiOperation("课程列表")
    @GetMapping("list")
    public R pageList(@ApiParam(value = "查询对象", required = true)
                              WebCourseQueryVo webCourseQueryVo) {

        List<Course> courseList = courseService.webSelectList(webCourseQueryVo);

        return R.ok().data("courseList", courseList);
    }

    @ApiOperation("根据id查询课程")
    @GetMapping("get/{courseId}")
    public R getById(@ApiParam(value = "课程id", required = true)
                     @PathVariable String courseId) {

        // 查询课程信息和讲师信息
        WebCourseVo webCourseVo = courseService.selectWebCourseVoById(courseId);

        // 查询当前课程的嵌套章节和课时信息
        List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);

        return R.ok().data("course", webCourseVo).data("chapterVoList", chapterVoList);
    }

    @ApiOperation("根据课程id查询课程信息")
    @GetMapping("inner/get-course-dto/{courseId}")
    public CourseDto getCourseDtoById(@ApiParam(value = "课程ID", required = true)
                                      @PathVariable String courseId) {

        CourseDto courseDto = courseService.getCourseDtoById(courseId);

        return courseDto;
    }

    @ApiOperation("根据课程id更改销售量")
    @GetMapping("inner/update-buy-count/{id}")
    public R updateBuyCountById(@ApiParam(value = "课程id", required = true)
                                @PathVariable String id) {

        courseService.updateBuyCountById(id);

        return R.ok();
    }

}
